package com.shiku.imserver.service;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.shiku.imserver.ImServerGroupContext;
import com.shiku.imserver.cluster.ImClusterServiceIfc;
import com.shiku.imserver.common.AbstractService;
import com.shiku.imserver.common.ImConfig;
import com.shiku.imserver.message.processor.MessageProcess;
import com.shiku.imserver.repository.GroupRepository;
import com.shiku.imserver.repository.MessageRepository;
import com.shiku.imserver.repository.NewFirendRepository;
import com.shiku.imserver.repository.ServiceRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class IMBeanManager extends AbstractService {
    public static Logger logger = LoggerFactory.getLogger(IMBeanManager.class);
    private ImServerGroupContext groupContext;
    private GroupRepository groupRepository = new GroupRepository();
    private ImClusterServiceIfc imClusterService;
    private ImConfig imConfig = null;
    private MessageProcess messageProcess = new MessageProcess();
    private MessageRepository messageRepository = new MessageRepository();
    private MongoClient mongoClient;
    private NewFirendRepository newFirendRepository = new NewFirendRepository();
    private ReceiptLogicService receiptLogicService = new ReceiptLogicService();
    private RedisService redisService = new RedisService();
    private RocketmqService rocketmqService = new RocketmqService();
    private ServiceRepository serviceRepository = new ServiceRepository();

    public boolean initialize() {
        try {
            this.mongoClient = new MongoClient(new MongoClientURI(IMBeanUtils.getImconfig().getMongoConfig().getUri()));
            this.serviceRepository.initialize();
            this.messageProcess.initialize();
            this.messageRepository.initialize();
            this.groupRepository.initialize();
            this.newFirendRepository.initialize();
            this.redisService.initialize();
            this.rocketmqService.initialize();
            this.receiptLogicService.initialize();
            if (this.imConfig.isCluster()) {
                try {
                    System.out.println(System.getProperty("java.ext.dirs"));
                    this.imClusterService = (ImClusterServiceIfc) Thread.currentThread().getContextClassLoader().loadClass(ImClusterServiceIfc.CLASSNAME).newInstance();
                    this.imClusterService.initialize();
                } catch (ClassNotFoundException e) {
                    logger.error("集群组建 初始化失败 找不到集群实现类 com.shiku.imserver.cluster.ImClusterService", e);
                    System.exit(0);
                } catch (Exception e2) {
                    logger.error(e2.getMessage(), e2);
                    System.exit(0);
                }
            }
            return true;
        } catch (Exception e3) {
            logger.error(" init mongoClient Exception  {} ", e3.getMessage());
            return false;
        }
    }

    public boolean startupAfter() {
        try {
            if (this.imClusterService == null) {
                return true;
            }
            this.imClusterService.startupAfter();
            return true;
        } catch (Exception e) {
            logger.error(e.getMessage());
            return true;
        }
    }

    public ImConfig getImConfig() {
        return this.imConfig;
    }

    public void setImConfig(ImConfig imConfig2) {
        this.imConfig = imConfig2;
    }

    public MessageProcess getMessageProcess() {
        return this.messageProcess;
    }

    public MongoClient getMongoClient() {
        return this.mongoClient;
    }

    public RedisService getRedisService() {
        return this.redisService;
    }

    public MessageRepository getMessageRepository() {
        return this.messageRepository;
    }

    public GroupRepository getGroupRepository() {
        return this.groupRepository;
    }

    public NewFirendRepository getNewFirendRepository() {
        return this.newFirendRepository;
    }

    public RocketmqService getRocketmqService() {
        return this.rocketmqService;
    }

    public ReceiptLogicService getReceiptLogicService() {
        return this.receiptLogicService;
    }

    public ServiceRepository getServiceRepository() {
        return this.serviceRepository;
    }

    public ImServerGroupContext getGroupContext() {
        return this.groupContext;
    }

    public void setGroupContext(ImServerGroupContext groupContext2) {
        this.groupContext = groupContext2;
    }

    public ImClusterServiceIfc getImClusterService() {
        return this.imClusterService;
    }

    public void setImClusterService(ImClusterServiceIfc imClusterService2) {
        this.imClusterService = imClusterService2;
    }
}
